package com.huilian.iotbox.admin.websocket;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ServerEndpoint("/webSocket/{key}")
@Slf4j
public class WebSocket {

    private Session session;

    private static ConcurrentHashMap<String, WebSocket> webSocketMap = new ConcurrentHashMap<String, WebSocket>();//建立连接的方法

    @OnOpen
    public void onOpen(Session session, @PathParam(value = "key") String key) {
        this.session = session;
        webSocketMap.put(key, this);
        log.info("【webSocket消息】有新的连接, 总数:{}", webSocketMap.size());
    }

    @OnError
    public void onError(Session session, Throwable thr) {
        webSocketMap.remove(this);
        log.info("【websocket消息】浏览器关闭，连接自动断开, 总数:{}", webSocketMap.size());
    }

    @OnClose
    public void onClose() {
        webSocketMap.remove(this);
        log.info("【websocket消息】连接主动断开, 总数:{}", webSocketMap.size());
    }

    @OnMessage
    public void onMessage(String message) {
        log.info("【websocket消息】收到客户端发来的消息:{}", message);
    }


    public void qrCodePayNotify(String message, String key) {
        log.info("发送websocket消息前：" + webSocketMap.keySet().size());
        try {
            Session session = webSocketMap.get(key).session;
            send(key,message,session);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void sendMessage(String message, String key) {
        try {
            Session session = null;
            WebSocket webSocket = null;
            log.info("发送websocket消息前："+webSocketMap.keySet().size());
            for (String strKey : webSocketMap.keySet()) {
                if (strKey.indexOf(key) != -1 || strKey.indexOf("admin") != -1) {
                    session = webSocketMap.get(strKey).session;
                    send(strKey,message,session);
                    continue;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMessageByKey(String message, String key) {
        try {
            Session session = null;
            WebSocket webSocket = null;
            log.info("发送websocket消息前："+webSocketMap.keySet().size());
            session = webSocketMap.get(key).session;
            if(session != null){
                send(key,message,session);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void send(String key, String message, Session session) throws IOException {
        if (session.isOpen()) {
            webSocketMap.get(key).session.getBasicRemote().sendText(message);
            log.info(key + "发送websocket消息：" + message);
        } else {
            webSocketMap.remove(key);
            log.info("移除websocket" + key);
        }
    }
}
